package creative.fire.interview.amazon.prepare;

import java.util.Stack;

/**
 * @author feuyeux@gmail.com 2011-3-16
 */
public class StackQueue<X> {

	private Stack<X> stackLeft;
	private Stack<X> stackRight;

	public StackQueue() {
		stackLeft = new Stack<X>();
		stackRight = new Stack<X>();
	}

	public X poll() {
		while (!stackLeft.empty()) {
			stackRight.push(stackLeft.pop());
		}
		if (!stackRight.empty())
			return stackRight.pop();
		return null;
	}

	public void offer(X x) {
		while (!stackRight.empty()) {
			stackLeft.push(stackRight.pop());
		}
		stackRight.push(x);
	}

	public static void main(String[] args) {
		StackQueue<Integer> sq = new StackQueue<Integer>();

		for (int i = 0; i < 10; i++) {
			sq.offer(i);
		}

		Integer x = null;
		x = sq.poll();
		while (x != null) {
			System.out.println(x);
			x = sq.poll();
		}

		sq.offer(1);
		sq.offer(2);
		System.out.println(sq.poll());
		sq.offer(3);
		sq.offer(4);
		sq.offer(5);
		System.out.println(sq.poll());
		System.out.println(sq.poll());
		sq.offer(6);
		sq.offer(7);
		System.out.println(sq.poll());
		System.out.println(sq.poll());
		System.out.println(sq.poll());
		System.out.println(sq.poll());
	}
}
